/*
 * MysqBindParam object defines the needed conversion of bind parameters
 * Copyright (C) 2002 Johnathan Ingram, jingram@rogue-order.net
 *
 * This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
 *
 */


#include "mysqlBindParam.h"
#include "dbconnectTypes.h"


// -----------------------------------------------------------------------------
// MysqlBindParam::paramToMySQLString
// -----------------------------------------------------------------------------
string 
MysqlBindParam::paramToMySQLString()
{
   // Check if the value is null and return the appropriate null string
   if (!valuePtr.ptr)
      return "NULL";
  
  
   string res;
   char* to;
   
   switch(type)
   {
      case BP_STRING:
         // Escape any funny characters in the string or binary data.

         // In the worse case, each character may need to be encoded as using two bytes, 
         to = (char*)malloc(valueSize*2 +1);
         mysql_escape_string(to, valuePtr.charPtr, valueSize);

         res = "'";
         res += to;       
         res += "'";
         free(to);
         break;      

      case BP_BINARY:                   
         // Escape any funny characters in the string or binary data.

         // In the worse case, each character may need to be encoded as using two bytes, 
         to = (char*)malloc(valueSize*2 +1);
         mysql_escape_string(to, (const char*)valuePtr.ptr, valueSize);

         res = "'";
         res += to;       
         res += "'";
         free(to);
         break;      
     
      case BP_LONG:
         to = (char*)malloc(512);
         sprintf(to, DBLONG_SPRINTF, *valuePtr.longPtr);
         res = to;       
         free(to);         
         break;      

      case BP_ULONG:
         to = (char*)malloc(512);
         sprintf(to, DBULONG_SPRINTF, *valuePtr.unsignedLongPtr);
         res = to;       
         free(to);         
         break;      

      case BP_DOUBLE:
         to = (char*)malloc(512);
         sprintf(to, "%.25E", *valuePtr.doublePtr);    // Use a precision of 25 for the conversion
         res = "'";
         res += to;       
         res += "'";
         free(to);         
         break;      
      
      case BP_DATETIME:        
         // 'yyyy-mm-dd hh:mm:ss' string format
         res = "'";
         res += valuePtr.JDatePtr->asString("%Y-%m-%d %H:%M:%S");
         res += "'";
         break;      

      case BP_DATE:
         // 'yyyy-mm-dd' string format
         res = "'";
         res += valuePtr.JDatePtr->asString("%Y-%m-%d");
         res += "'";
         break;      

      case BP_TIME:
         // 'hh:mm:ss' string format
         res = "'";
         res += valuePtr.JDatePtr->asString("%H:%M:%S");
         res += "'";
         break;      

      case BP_BOOLEAN:
         // Convert a boolean to an integer value.
         if (*valuePtr.boolPtr)
            res = "1";
         else
            res = "0";
         break;
   }

   return res;
}  // MysqlBindParam::paramToMySQLString


